package com.inzone.fram.gray.common.filter;

import com.inzone.fram.gray.common.rule.MatchingContext;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author 丁常磊
 * @date 2021/10/15 18:54
 */

public class InitMatchingContextFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        if (servletRequest instanceof HttpServletRequest) {
            try {
                HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
                MatchingContext context = MatchingContext.init();
                context.setTagsText(httpServletRequest.getHeader(MatchingContext.GRAY_HEADER_TAGS));
                context.setGroupsText(httpServletRequest.getHeader(MatchingContext.GRAY_HEADER_GROUPS));
                filterChain.doFilter(servletRequest, servletResponse);
            } finally {
                MatchingContext.reset();
            }
        }
    }
}
